From 1a0a5d0399d5b3aaa4edc1c9763e07c0c35d1048 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Wed, 27 Oct 1999 17:28:44 +0000 Subject: [PATCH] New function to create a blank pixbuf. 1999-10-27 Havoc Pennington * src/gdk-pixbuf.c (gdk_pixbuf_new): New function to create a blank pixbuf. * src/gdk-pixbuf-loader.c (gdk_pixbuf_loader_write): Check all three progressive load funcs are non-NULL, rather than checking begin_load three times. Also, check whether begin_load returns NULL on failure. --- gdk-pixbuf/gdk-pixbuf-loader.c | 11 ++++-- gdk-pixbuf/gdk-pixbuf.c | 61 ++++++++++++++++++++++++++++++++++ gdk-pixbuf/gdk-pixbuf.h | 3 ++ gtk/gdk-pixbuf-loader.c | 11 ++++-- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/gdk-pixbuf/gdk-pixbuf-loader.c b/gdk-pixbuf/gdk-pixbuf-loader.c index ef6c076094..a86736e0e9 100644 --- a/gdk-pixbuf/gdk-pixbuf-loader.c +++ b/gdk-pixbuf/gdk-pixbuf-loader.c @@ -240,14 +240,19 @@ gdk_pixbuf_loader_write (GdkPixbufLoader *loader, gchar *buf, gint count) if (priv->image_module == NULL) { return FALSE; } else if ((priv->image_module->begin_load == NULL) || - (priv->image_module->begin_load == NULL) || - (priv->image_module->begin_load == NULL) || - (priv->image_module->begin_load == NULL)) { + (priv->image_module->stop_load == NULL) || + (priv->image_module->load_increment == NULL)) { g_warning ("module %s does not support incremental loading.\n", priv->image_module->module_name); return FALSE; } else { g_print ("module loaded: name is %s\n", priv->image_module->module_name); priv->context = (priv->image_module->begin_load) (gdk_pixbuf_loader_prepare, loader); + + if (priv->context == NULL) { + g_warning("Failed to begin progressive load"); + return FALSE; + } + retval = (priv->image_module->load_increment) (priv->context, priv->buf, 128); /* if we had more then 128 bytes total, we want to send the rest of the buffer */ diff --git a/gdk-pixbuf/gdk-pixbuf.c b/gdk-pixbuf/gdk-pixbuf.c index a35ac01c46..924f987bcc 100644 --- a/gdk-pixbuf/gdk-pixbuf.c +++ b/gdk-pixbuf/gdk-pixbuf.c @@ -99,3 +99,64 @@ gdk_pixbuf_new_from_art_pixbuf (ArtPixBuf *art_pixbuf) return pixbuf; } + +/** + * gdk_pixbuf_new: + * @art_pixbuf: A libart pixbuf. + * + * Creates a &GdkPixbuf; magically sets the ArtPixFormat, rowstride, and creates + * the buffer. Use gdk_pixbuf_new_from_data() to do things manually. + * + * Return value: A newly-created &GdkPixbuf structure with a reference count of + * 1. Somewhat oddly, returns NULL if it can't allocate the buffer; this unusual + * behavior is needed because images can be very large. + **/ +GdkPixbuf * +gdk_pixbuf_new (gboolean has_alpha, int width, int height) +{ + GdkPixbuf *pixbuf; + + g_return_val_if_fail (width > 0, NULL); + g_return_val_if_fail (height > 0, NULL); + + pixbuf = g_new (GdkPixbuf, 1); + pixbuf->ref_count = 1; + + if (has_alpha) { + art_u8* pixels; + int rowstride; + + /* FIXME, pick an optimal stride */ + rowstride = 4*width; + + pixels = art_alloc(rowstride*height); + + if (pixels == NULL) { + g_free(pixbuf); + return NULL; + } + + pixbuf->art_pixbuf = art_pixbuf_new_rgba(pixels, width, height, rowstride); + } else { + art_u8* pixels; + int rowstride; + + /* FIXME, pick an optimal stride */ + rowstride = 3*width; + + pixels = art_alloc(rowstride*height); + + if (pixels == NULL) { + g_free(pixbuf); + return NULL; + } + + pixbuf->art_pixbuf = art_pixbuf_new_rgb(pixels, width, height, rowstride); + } + + return pixbuf; + + +} + + diff --git a/gdk-pixbuf/gdk-pixbuf.h b/gdk-pixbuf/gdk-pixbuf.h index ec42cf3eae..47fce21777 100644 --- a/gdk-pixbuf/gdk-pixbuf.h +++ b/gdk-pixbuf/gdk-pixbuf.h @@ -59,6 +59,9 @@ void gdk_pixbuf_unref (GdkPixbuf *pixbuf); GdkPixbuf *gdk_pixbuf_new_from_art_pixbuf (ArtPixBuf *art_pixbuf); +/* Create a "blank" pixbuf with an optimal rowstride and a new buffer */ +GdkPixbuf *gdk_pixbuf_new (gboolean has_alpha, int width, int height); + /* Simple loading */ GdkPixbuf *gdk_pixbuf_new_from_file (const char *filename); diff --git a/gtk/gdk-pixbuf-loader.c b/gtk/gdk-pixbuf-loader.c index ef6c076094..a86736e0e9 100644 --- a/gtk/gdk-pixbuf-loader.c +++ b/gtk/gdk-pixbuf-loader.c @@ -240,14 +240,19 @@ gdk_pixbuf_loader_write (GdkPixbufLoader *loader, gchar *buf, gint count) if (priv->image_module == NULL) { return FALSE; } else if ((priv->image_module->begin_load == NULL) || - (priv->image_module->begin_load == NULL) || - (priv->image_module->begin_load == NULL) || - (priv->image_module->begin_load == NULL)) { + (priv->image_module->stop_load == NULL) || + (priv->image_module->load_increment == NULL)) { g_warning ("module %s does not support incremental loading.\n", priv->image_module->module_name); return FALSE; } else { g_print ("module loaded: name is %s\n", priv->image_module->module_name); priv->context = (priv->image_module->begin_load) (gdk_pixbuf_loader_prepare, loader); + + if (priv->context == NULL) { + g_warning("Failed to begin progressive load"); + return FALSE; + } + retval = (priv->image_module->load_increment) (priv->context, priv->buf, 128); /* if we had more then 128 bytes total, we want to send the rest of the buffer */ -- 2.30.2